package com.fa.wisdom.ui;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.util.Log;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bumptech.glide.Glide;
import com.coder.zzq.smartshow.dialog.LoadingDialog;
import com.fa.wisdom.R;
import com.fa.wisdom.base.BaseActivity;
import com.fa.wisdom.base.Constant;
import com.fa.wisdom.common.App;
import com.fa.wisdom.model.ResultEntity;
import com.fa.wisdom.net.MyCallback;
import com.fa.wisdom.net.Task;
import com.fa.wisdom.net.Url;
import com.fa.wisdom.utils.Loading;
import com.fa.wisdom.utils.RichTextUtils;
import com.fa.wisdom.utils.SpUtils;


import retrofit2.Response;

public class GoodsFDetailActivity extends BaseActivity {
    RelativeLayout ll_titleview;
    TextView title_mid, tv_title, tv_current_m, tv_have_m, tv_paid, tv_kc, tv_content;
    ImageView title_left, iv_head;
    WebView webView;
    private String goodId;

    @Override
    public void setListen() {
        tv_title = findViewById(R.id.tv_title);//商品名称
        tv_current_m = findViewById(R.id.tv_current_m);//当前价格
        tv_have_m = findViewById(R.id.tv_have_m);//之前价格
        tv_paid = findViewById(R.id.tv_paid);//已经卖出
        tv_kc = findViewById(R.id.tv_kc);//库存
        tv_content = findViewById(R.id.tv_content);//富文本 内容
        iv_head = findViewById(R.id.iv_head);//图片
        title_left=findViewById(R.id.title_left);
        title_left.setImageResource(R.drawable.icon_return_white);
        webView=findViewById(R.id.webView);
    }

    @Override
    public void setTitleBarColor() {

    }

    @Override
    public int intiLayout() {
        return R.layout.activity_goods_detail;
    }

    @Override
    public void initView() {
        ll_titleview = findViewById(R.id.rl_titlebar);
        ll_titleview.setBackgroundColor(Color.parseColor("#1E96E3"));
        title_mid = findViewById(R.id.title_mid);
        title_mid.setText("商品详情");
        title_mid.setTextColor(Color.parseColor("#ffffff"));
        title_left = findViewById(R.id.title_left);
        title_left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void initData() {
        goodId = getIntent().getStringExtra("goodId");
        int userId = SpUtils.getInstance().decodeInt(Constant.User_id, 0);
        LoadingDialog dialog = Loading.getInstance().onShowLargeLoading("努力加载中...");
        dialog.showInActivity(this);
        Task.getApiService().getGoodDetail(goodId, userId).enqueue(new MyCallback<ResultEntity>() {
            @Override
            protected void onSuccess(Response<ResultEntity> response) {
                dialog.dismiss();
                String jsonString = JSON.toJSONString(response.body());
                JSONObject jsonObject = JSON.parseObject(jsonString);
                JSONObject jsonObject1 = jsonObject.getJSONObject("data");
                String name=jsonObject1.getString("goodsname");//商品名称
                String currentM=jsonObject1.getString("price");//当前价格
                String haveM=jsonObject1.getString("productprice");//过去价格
                String content = jsonObject1.getJSONObject("get_content").getString("content");
                int showsales=jsonObject1.getInteger("showsales");//已售
                int seller_count=jsonObject1.getInteger("seller_count");//库存
                tv_title.setText(name);
                tv_current_m.setText("￥"+currentM);
                tv_have_m.setText("￥"+haveM);
                tv_paid.setText("已售"+showsales);
                tv_kc.setText("库存"+seller_count);
                tv_have_m.getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG); //中划线
                String img= Url.ImgUrl +jsonObject1.getJSONArray("getImage").getJSONObject(0).getString("image");
                Glide.with(App.getContext()).load(img)
                        .into(iv_head);

                webView.getSettings().setJavaScriptEnabled(true);
                webView.getSettings().setBuiltInZoomControls(true);
                webView.getSettings().setDisplayZoomControls(false);
                webView.setWebChromeClient(new WebChromeClient());
                webView.setWebViewClient(new WebViewClient());
                webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY); //取消滚动条白边效果
                webView.getSettings().setDefaultTextEncodingName("UTF-8") ;
                webView.getSettings().setBlockNetworkImage(false);
                if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
                    webView.getSettings().setMixedContentMode(webView.getSettings()
                            .MIXED_CONTENT_ALWAYS_ALLOW);  //注意安卓5.0以上的权限
                }
                webView.loadDataWithBaseURL(null, content, "text/html", "UTF-8", null);
            }

            @Override
            protected void onFailure(Throwable t) {
                dialog.dismiss();
            }
        });
    }
}
